package com.lgx.mall.core.controller;

import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lgx.common.entity.Dictionary;
import com.lgx.common.exception.DataException;
import com.lgx.common.tree.easyui.TreeModel;
import com.lgx.common.web.json.JsonUtils;
import com.lgx.common.web.util.DictUtils;
import com.lgx.common.web.util.EscapeUtils;
import com.lgx.mall.core.entity.Category;
import com.lgx.mall.core.service.ICategoryService;

@Controller
@RequestMapping("/category")
public class CategoryController {

	@Autowired
	private ICategoryService categoryService;
	
	public ICategoryService getCategoryService() {
		return categoryService;
	}

	public void setCategoryService(ICategoryService categoryService) {
		this.categoryService = categoryService;
	}

	@RequestMapping("/categoryMgr.do")
	public String categoryMgr(HttpServletRequest request,HttpServletResponse response)
	{
		return "mall/core/category/categoryMgr";
	}
	
	@RequestMapping("/list.do")
	public void list(HttpServletRequest request,HttpServletResponse response)
	{
		List<Category> ca = categoryService.findCategoryList();
		JsonUtils utils = new JsonUtils();
		String json;
		try {
			json = utils.converToJson(ca);
			response.setCharacterEncoding("UTF-8");
			PrintWriter out = response.getWriter();
			out.print(json);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping("/init.do")
	public String init(HttpServletRequest request,HttpServletResponse response)
	{
		String type = request.getParameter("type");
		String categoryId = request.getParameter("categoryId");
		if(type.equals("edit")){
			Category category = this.categoryService.findCategoryById(Long.parseLong(categoryId));
			request.setAttribute("category", category);
		}
		
		//是否显示
		List<Dictionary> sendMsg = DictUtils.getDictListByKind(DictUtils.SEND_MSG);
		request.setAttribute("sendMsg", sendMsg);
		
		request.setAttribute("type", type);
		request.setAttribute("categoryId", categoryId);
		
		return "mall/core/category/category-init";
	}
	
	@RequestMapping("/save.do")
	@ResponseBody
	public String save(HttpServletRequest request,HttpServletResponse response)
	{
		String categoryId = request.getParameter("categoryId");
		Category parent = this.categoryService.findCategoryById(Long.parseLong(categoryId));
		String name = request.getParameter("name");
		if(!StringUtils.isEmpty(name))name = EscapeUtils.unescape(name);
		String description = request.getParameter("description");
		if(!StringUtils.isEmpty(description))description = EscapeUtils.unescape(description);
		String sendMsg = request.getParameter("sendMsg");
		if(!StringUtils.isEmpty(sendMsg))sendMsg = EscapeUtils.unescape(sendMsg);
		String url = request.getParameter("url");
		if(!StringUtils.isEmpty(url))url = EscapeUtils.unescape(url);
		String pCount = request.getParameter("pCount");
		if(!StringUtils.isEmpty(pCount)){
			pCount = EscapeUtils.unescape(pCount);
		}else{
			pCount = "0";
		}
		
		Category c = new Category();
		c.setName(name);
		c.setParent(parent);
		c.setUrl(url);
		c.setDescription(description);
		c.setSendMsg(sendMsg);
		c.setPcount(Integer.parseInt(pCount));
		
		try {
			this.categoryService.createCategory(c);
		} catch (DataException e1) {
			e1.printStackTrace();
		}
		
		request.setAttribute("message", "操作成功！");
		return "1";
	}
	
	@RequestMapping("/update.do")
	@ResponseBody
	public String update(HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException
	{
		String categoryId = request.getParameter("categoryId");
		Category c = this.categoryService.findCategoryById(Long.parseLong(categoryId));
		
		String name = request.getParameter("name");
		if(!StringUtils.isEmpty(name))name = EscapeUtils.unescape(name);
		String description = request.getParameter("description");
		if(!StringUtils.isEmpty(description))description = EscapeUtils.unescape(description);
		String sendMsg = request.getParameter("sendMsg");
		if(!StringUtils.isEmpty(sendMsg))sendMsg = EscapeUtils.unescape(sendMsg);
		String url = request.getParameter("url");
		if(!StringUtils.isEmpty(url))url = EscapeUtils.unescape(url);
		String layerOrder = request.getParameter("layerOrder");
		if(!StringUtils.isEmpty(layerOrder))layerOrder = EscapeUtils.unescape(layerOrder);
		String pCount = request.getParameter("pCount");
		if(!StringUtils.isEmpty(pCount))pCount = EscapeUtils.unescape(pCount);
		
		c.setName(name);
		c.setUrl(url);
		c.setDescription(description);
		c.setSendMsg(sendMsg);
		c.setLayerOrder(layerOrder);
		c.setPcount(Integer.parseInt(pCount));
		
		try {
			this.categoryService.updateCategory(c);
		} catch (DataException e1) {
			e1.printStackTrace();
		}
		
		request.setAttribute("message", "操作成功！");
		return "1";
	}
	
	@RequestMapping("/delete.do")
	@ResponseBody
	public String delete(HttpServletRequest request,HttpServletResponse response)
	{
		String categoryId = request.getParameter("categoryId");
		try {
			this.categoryService.deleteCategory(Long.parseLong(categoryId));
			return "1";
		} catch (DataException e1) {
			e1.printStackTrace();
		}
		return "0";
	}
	
	@RequestMapping("/listToCombotree.do")
	public void listToCombotree(HttpServletRequest request,HttpServletResponse response)
	{
		List<TreeModel> ca = categoryService.findCategoryListToCombotree();
		JsonUtils utils = new JsonUtils();
		String json;
		try {
			json = utils.converToJson(ca);
			response.setCharacterEncoding("UTF-8");
			PrintWriter out = response.getWriter();
			out.print(json);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
